home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
C and C++
/
Libraries
/
usr (gcc 1.37 libs)
/
mac
/
read.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-12-08
|
1KB
|
50 lines
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include "crtlocal.h"
ssize_t read(int fd, void *buf, size_t size)
{
if (crt_fd_tab[fd].fd==0) /* stdin */
{
int wait = 1;
int cnt = 0;
while ((cnt < size) && wait)
{
int ch = cgetc();
char c = ch;
switch(ch)
{
case EOF: return EOF; break;
case '\b': if (cnt >0) cnt--; break;
case '\r': ch = '\n';
case '\n': wait = 0;
default: ((char *)buf)[cnt++] = ch; break;
}
cwrite(0, &c, 1);
}
return cnt;
}
else if (crt_fd_tab[fd].flags & O_PIPE)
{
return readpipe(fd, buf, size);
}
else
{
int i;
OSErr err;
IOParam pb;
pb.ioCompletion = 0;
pb.ioRefNum = crt_fd_tab[fd].fd;
pb.ioBuffer = buf;
pb.ioReqCount = size;
pb.ioPosMode = fsAtMark;
PBReadSync((ParmBlkPtr)&pb);
err = pb.ioResult;
if (crt_fd_tab[fd].flags & O_TEXT)
for (i = pb.ioActCount; i--; ) if (((char *)buf)[i] == '\r') ((char *)buf)[i] = '\n';
mysleep(1);
return pb.ioActCount;
}
}